{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "smaller-feeding",
   "metadata": {},
   "source": [
    "# Lithuanian Personal Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "challenging-perspective",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "determined-junior",
   "metadata": {},
   "source": [
    "The function `clean_lt_asmens()` cleans a column containing Lithuanian personal number (Asmens koda) strings, and standardizes them in a given format. The function `validate_lt_asmens()` validates either a single Asmens koda strings, a column of Asmens koda strings or a DataFrame of Asmens koda strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "forbidden-connection",
   "metadata": {},
   "source": [
    "Asmens koda strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"33309240064\"\n",
    "* `standard`: Asmens koda strings with proper whitespace in the proper places. Note that in the case of Asmens koda, the compact format is the same as the standard one.\n",
    "* `birthdate`: split the date parts from the number and return the birth date, like \"1933-09-24\".\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_lt_asmens()` and `validate_lt_asmens()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "signal-leave",
   "metadata": {},
   "source": [
    "### An example dataset containing Asmens koda strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "hidden-crawford",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"asmens\": [\n",
    "            '33309240064',\n",
    "            '33309240164',\n",
    "            '7542011030',\n",
    "            '7552A10004',\n",
    "            '8019010008',\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\",\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"1111 S Figueroa St, Los Angeles, CA 90015\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "understood-composer",
   "metadata": {},
   "source": [
    "## 1. Default `clean_lt_asmens`\n",
    "\n",
    "By default, `clean_lt_asmens` will clean asmens strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bigger-counter",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_lt_asmens\n",
    "clean_lt_asmens(df, column = \"asmens\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "honey-death",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "organized-parish",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "molecular-fields",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "academic-monaco",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_lt_asmens(df, column = \"asmens\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "intermediate-mailing",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "neutral-convergence",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_lt_asmens(df, column = \"asmens\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "remarkable-california",
   "metadata": {},
   "source": [
    "### `birthdate`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "global-standing",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_lt_asmens(df, column = \"asmens\", output_format=\"birthdate\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "lesser-assignment",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned Asmens koda strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "intermediate-tribune",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_lt_asmens(df, column=\"asmens\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cordless-giving",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "superb-union",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "automated-bullet",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_lt_asmens(df, \"asmens\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "applicable-rescue",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "happy-nevada",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_lt_asmens(df, \"asmens\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "political-finish",
   "metadata": {},
   "source": [
    "## 4. `validate_lt_asmens()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "secure-active",
   "metadata": {},
   "source": [
    "`validate_lt_asmens()` returns `True` when the input is a valid Asmens koda. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_lt_asmens()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_lt_asmens()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_lt_asmens()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "editorial-satin",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_lt_asmens\n",
    "print(validate_lt_asmens('33309240064'))\n",
    "print(validate_lt_asmens('33309240164'))\n",
    "print(validate_lt_asmens('7542011030'))\n",
    "print(validate_lt_asmens('7552A10004'))\n",
    "print(validate_lt_asmens('8019010008'))\n",
    "print(validate_lt_asmens(\"hello\"))\n",
    "print(validate_lt_asmens(np.nan))\n",
    "print(validate_lt_asmens(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "removed-outline",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "polished-tribute",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_lt_asmens(df[\"asmens\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "agricultural-courage",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "imposed-creation",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_lt_asmens(df, column=\"asmens\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "welcome-shelter",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "choice-romantic",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_lt_asmens(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "enabling-landing",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
